package com.future.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.future.domain.Electric;
import com.future.domain.Electricdetails;
import com.future.dto.ElectricDto;
import com.future.dto.ElectricdetailsDto;
import com.future.dto.Moneys;
import com.future.dto.MsgDto;
import com.future.service.ElectricService;
import com.future.service.ElectricdetailsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: 何晔
 * @time: 2022/6/29 13:49
 */
@RestController
@RequestMapping("Electricdetails")
public class ElectricdetailsController {


    @Autowired
    private ElectricdetailsService  electricdetailsService;
    @Autowired
    ElectricService  electricService;



    //开始充值
    @RequestMapping("pay")
    public MsgDto pay(ElectricdetailsDto electricdetailsDto){

        //1.宿舍id     electricdetailsDto.getElectricroomid
        //2.充值金额    electricdetailsDto.getrechargeamount
        //3.充值人       electricdetailsDto.getrechargename
        //4.充值日期   new Date();
        //5.充值方式  ：微信支付
        //6.收款单位   天坑坑有限公司

        System.out.println(electricdetailsDto);

        Date  date   =   new Date();
        String   rechargemethood  =  "微信支付";
        String   payeeMan   =   "天坑坑有限公司";
        Electricdetails  electricdetails  =  new Electricdetails();
        electricdetails.setElectricroomid(electricdetailsDto.getElectricroomid());
        electricdetails.setRechargeamount(electricdetailsDto.getRechargeamount());
        electricdetails.setRechargename(electricdetailsDto.getRechargename());
        electricdetails.setRechargedate(date);
        electricdetails.setRechargemethod(rechargemethood);
        electricdetails.setPayeeman(payeeMan);

        this.electricdetailsService.save(electricdetails);

        // 根据 宿舍 id  获取  宿舍电费 对象
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("electricRoomId",electricdetailsDto.getElectricroomid());
        Electric   electric  =   this.electricService.getOne(queryWrapper);



        electric.setRemainingelectricity(electric.getRemainingelectricity()+electricdetailsDto.getRechargeamount());

        // 该宿舍增加 余额


        this.electricService.updateById(electric);

        if(electric.getRemainingelectricity()>0){
            electric.setElectricstatus("正常使用");
            this.electricService.updateById(electric);
        }
        return   new MsgDto(true,"充值成功");

    }




    //分业查询明细
    @RequestMapping("select")
    public PageInfo<ElectricDto> select(ElectricDto electricDto,int roomid){


        PageHelper.startPage(electricDto.getCurrent(),electricDto.getSize());
        List list    =   this.electricdetailsService.selectElectricdetails(roomid);

        PageInfo pageInfo=new PageInfo(list);


        return pageInfo;
    }

    //根据宿舍id查询
    @RequestMapping("selectRoomId")
    public MsgDto selectRoomId(int electricroomid){

        System.out.println(electricroomid);
       List<Moneys>  list   =  this.electricdetailsService.selectMoney(electricroomid);

        return   new MsgDto(true,"查询成功",list);
    }

}
